package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.mapper.CheckGroupDao;
import com.itheima.health.mapper.CheckItemDao;
import com.itheima.health.mapper.JianchazuMapper;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.model.pojos.CheckItem;
import com.itheima.health.service.JianchazuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class JianchazuControllerImp  implements JianchazuService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private JianchazuMapper JianchazuMapper;

    /* @Autowired
     private JianchazuMapper jianchazuMapper;*/
    @Override
    public ResultPageData fenye(PageParam pageParam) {
        Integer currentPage = pageParam.getCurrentPage();
        Integer pageSize = pageParam.getPageSize();
        IPage<CheckGroup> page = new Page<>(currentPage, pageSize);

        LambdaQueryWrapper<CheckGroup> query = new LambdaQueryWrapper<>();

        IPage<CheckGroup> pageData = checkGroupDao.selectPage(page, query);
        long total = pageData.getTotal();
        List<CheckGroup> records = pageData.getRecords();

        ResultPageData resultPageData = new ResultPageData();
        resultPageData.setTotal(total);
        resultPageData.setRows(records);
        return resultPageData;
    }

    @Override
    public void xinzeng(CheckGroup dto, String ids) {
        Integer groupId = JianchazuMapper.zengjia(dto);

// 将传入的String类型的ids转换成List<Integer>类型的idList
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(s -> s.matches("\\d+")) // 确保每个字符串都是数字
                .map(Integer::parseInt)
                .collect(Collectors.toList());

// 使用返回的 ID 和 ID 列表来保存关联记录
        if (groupId != null && !idList.isEmpty()) {
            List<Map<String, Object>> records = idList.stream()
                    .map(id -> {
                        Map<String, Object> record = new HashMap<>();
                        record.put("checkgroup_id", groupId);
                        record.put("checkitem_id", id);
                        return record;
                    })
                    .collect(Collectors.toList());
            JianchazuMapper.zengjia1(records);
        }

    }

    /*
     * 编辑先回显
     * */
    @Override
    public CheckGroup huixian(Integer id) {
        return JianchazuMapper.huixian(id);
    }

    @Override
    public void update(Integer[] checkItemIds, CheckGroup dto) {
        // 1.更新checkgroup

        JianchazuMapper.update(dto);

        //删除checkgroup与checkitem的关联关系
        JianchazuMapper.deleteCheckGroupCheckItemRelation(dto.getId());

        //新增checkgroup与checkitem的关联关系
        if (checkItemIds!=null && checkItemIds.length!=0){
            for (Integer checkItemId : checkItemIds) {
                JianchazuMapper.insertCheckGroupCheckItemRelation(dto.getId(), checkItemId);

            }
        }
    }

}















